package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.Dynamic;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class MyCourse extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String token = webUtil.getCookie(request, "LoginCookie");
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		int onepagecount = TextUtil.String2int(request.getParameter("onepagecount"), 10);
		
		Connection conn = DBUtil.getConnection();
		if (conn != null) {
			DAOFactory dao = DAOFactory.init(conn, request, response);
			UserDao udao = dao.getUserDao();
			DynamicDao ddao = dao.getDynamicDao();
			UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
			
			//其他地方登陆了
			if (info!=null) {
				int uid = info.getId();
				ArrayList<Dynamic> data =  ddao.getMySystemDynamic(uid, page, onepagecount);;
				
				if (data != null) {
					request.setAttribute("data", data);
					Repost.success("mycourse.jsp", request,response);
				} else {
					Repost.nologin(response);
				}
			} else {
				Repost.nologin(response);
			}
			
		} else {
			Repost.dberror(request, response);
		}
	}

}
